
#import <ansi/stdlib.h>
#import <ansi/string.h>
#import "IPPanel.h"

static const char *PANELPREFIX = "IPPANEL";

@interface IPPanel (IPPanelPrivateMethods)

- (void)_saveVisibleUsingName:(const char *)name isVisible:(BOOL)flag;

@end


@implementation IPPanel

+ (BOOL)isPanelSavedAsVisible:(const char *)name
{
	const char	*state;
	char *vname;
	
	if(name != NULL){
		vname = (char *)NXZoneMalloc([self zone], strlen(name) + strlen(PANELPREFIX) + 1);
		strcpy(vname, PANELPREFIX);
		strcat(vname, name);
		state = NXGetDefaultValue([NXApp appName], vname);
		if( state != NULL && atoi( state ) > 0 )
			return YES;
	}
	return NO;
}

- initContent:(const NXRect *)contentRect
        style:(int)aStyle
      backing:(int)bufferingType
   buttonMask:(int)mask
        defer:(BOOL)flag
{
	[super initContent:contentRect
	             style:aStyle
               backing:bufferingType
	        buttonMask:mask
 	             defer:flag];				 
	_visibleName = NULL;
	return self;
}


- free
{
	free(_visibleName);
	return [super free];
}

- (BOOL)setVisibleAutosaveName:(const char *)name
{
	if(name != NULL){
		if(_visibleName)
			_visibleName = (char *)NXZoneRealloc([self zone], _visibleName, strlen(name) + 1);
		else
			_visibleName = (char *)NXZoneMalloc([self zone], strlen(name) + 1);
		strcpy(_visibleName, name);
		if(![self setVisibleUsingName:_visibleName])
			[self saveVisibleUsingName:_visibleName];
	} else {
		if(_visibleName)
			NX_FREE(_visibleName);
		_visibleName = NULL;
	}	
	return YES;
}

- (BOOL)setVisibleAutosaveLikeFrameName
{
	return [self setVisibleAutosaveName:[self frameAutosaveName]];
}

- (const char *)visibleAutosaveName
{
	return _visibleName;
}


- (void)_saveVisibleUsingName:(const char *)name isVisible:(BOOL)flag
{
	char *vname;

	if(name != NULL){
		vname = (char *)NXZoneMalloc([self zone], strlen(name) + strlen(PANELPREFIX) + 1);
		strcpy(vname, PANELPREFIX);
		strcat(vname, name);
		if(flag)
			NXWriteDefault([NXApp appName], vname, "1");
		else
			NXWriteDefault([NXApp appName], vname, "0");
		NX_FREE(vname);
	}
}


- (void)saveVisibleUsingName:(const char *)name
{
	[self _saveVisibleUsingName:name isVisible:[self isVisible]];
}


- (BOOL)setVisibleUsingName:(const char *)name
{
	const char *state;
	char *vname;
	
	if(name != NULL){
		vname = (char *)NXZoneMalloc([self zone], strlen(name) + strlen(PANELPREFIX) + 1);
		strcpy(vname, PANELPREFIX);
		strcat(vname, name);
		state = NXGetDefaultValue([NXApp appName], vname);
		NX_FREE(vname);
		if(state != NULL){
			if(atoi(state))
				[super orderFront:self];
			else
				[super orderOut:self];
			return YES;
		}
	}
	return NO;
}


- windowWillClose:sender
{
	if(_visibleName != NULL) 
		[self _saveVisibleUsingName:_visibleName isVisible:0];
	return self;
}


- orderBack:sender
{
	[super orderBack:sender];
	if(_visibleName != NULL)
		[self saveVisibleUsingName:_visibleName];
	return self;
}


- orderFront:sender
{
	[super orderFront:sender];
	if(_visibleName != NULL)
		[self saveVisibleUsingName:_visibleName];
	return self;
}


- makeKeyAndOrderFront:sender
{
	[super makeKeyAndOrderFront:sender];
	if(_visibleName != NULL)
		[self saveVisibleUsingName:_visibleName];
	return self;
}


- orderFrontRegardless
{
	[super orderFrontRegardless];
	if(_visibleName != NULL)
		[self saveVisibleUsingName:_visibleName];
	return self;
}

@end
